package com.janwen.j2se.proxy;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CGLibProxy implements MethodInterceptor {

	static final Log logger = LogFactory.getLog(CGLibProxy.class);
	@Override
	public Object intercept(Object obj, Method method, Object[] args,
			MethodProxy proxy) throws Throwable {
		logger.info("begin intercept method log");
		Object o = proxy.invokeSuper(obj, args);
		logger.info("end intercept method log");
		return o;
	}

	public Object getProxy(Class clazz) {
		enhancer.setSuperclass(clazz);
		enhancer.setCallback(this);
		return enhancer.create();
	}

	private Enhancer enhancer = new Enhancer();
}
